const { util, BaseErr } = require("utils");
const {
    Type_Wx_Mini,
    Type_Wx_Web,
    Type_Qw_Mini,
    Type_Qw_Web,
    Type_DingDing_Web,

    Config_TokenType,
    Config_AppId,
    Config_AppSecret,
} = require("../constEnum.js");

const { getWxMiniToken } = require("./wx_mini/wxMiniToken.js");
const { getDDWebToken } = require("./dingding_web/dingdingWebToken.js");
const { getQYWxMiniToken } = require("./qy_mini/qyMiniToken.js");
const { getWxWebToken } = require("./wx_web/wxWebToken.js");
const { getQYWxWebToken } = require("./qy_web/qyWebToken.js");
async function getTokenByConfig(config) {
    let totalConfig = config || {};
    let type = totalConfig[Config_TokenType];
    let appId = totalConfig[Config_AppId];
    let appSecret = totalConfig[Config_AppSecret];
    if (type === Type_DingDing_Web) {
        // 钉钉
        return await getDDWebToken(appId, appSecret);
    } else if (type === Type_Wx_Mini) {
        // 微信小程序
        return await getWxMiniToken(appId, appSecret);
    } else if (type === Type_Wx_Web) {
        // 微信公众号
        return await getWxWebToken(appId, appSecret);
    } else if (type === Type_Qw_Mini) {
        // 企微小程序
        return await getQYWxMiniToken(appId, appSecret);
    } else if (type === Type_Qw_Web) {
        return await getQYWxWebToken(appId, appSecret);
    } else {
        let err = new BaseErr(-4000, `uni-config-center中 ${Config_TokenType} 类型错误`);
        return [util.resErr(err), null];
    }
}

module.exports = getTokenByConfig;
